﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;

namespace YArchitech.HVAC
{
	public partial class ResCoeLocalForm : System.Windows.Forms.Form
	{
		public IList<ValveAndFitting> ValveSet
		{
			get
			{
				return this.valveSet;
			}
		}

		public ResCoeLocalForm()
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
		}

		public ResCoeLocalForm(IList<ValveAndFitting> valves) : this()
		{
			this.valveSet = valves;
		}

		private void ResCoeLocalForm_Load(object sender, EventArgs e)
		{
			this.InitializeDataGridView();
			this.DataGridAddInfos();
		}

		private void InitializeDataGridView()
		{
			this.dgvValveInfo.Columns.Add(this.CreatColumn("名称", true, 180, true));
			this.dgvValveInfo.Columns.Add(this.CreatColumn("类型", true, 75, false));
			this.dgvValveInfo.Columns.Add(this.CreatColumn("局阻系数", false, 60, false));
			this.dgvValveInfo.Columns.Add(this.CreatColumn("局阻(Pa)", false, 60, false));
		}

		private DataGridViewTextBoxColumn CreatColumn(string header, bool readOnly, int minWidth, bool bFrozen = false)
		{
			return new DataGridViewTextBoxColumn
			{
				AutoSizeMode = (bFrozen ? DataGridViewAutoSizeColumnMode.None : DataGridViewAutoSizeColumnMode.Fill),
				Frozen = bFrozen,
				MinimumWidth = minWidth,
				ReadOnly = readOnly,
				HeaderText = header
			};
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.SaveData();
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private void bynCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void DataGridAddInfos()
		{
			for (int i = 0; i < this.valveSet.Count; i++)
			{
				object[] values = new object[]
				{
					this.valveSet[i].Name,
					this.valveSet[i].TypeName,
					this.valveSet[i].ResCoefLocal.ToString("F2"),
					this.valveSet[i].ResistanceLocal.ToString("F3")
				};
				this.dgvValveInfo.Rows.Add(values);
				this.dgvValveInfo.Rows[this.dgvValveInfo.RowCount - 1].Tag = this.valveSet[i];
			}
		}

		private void SaveData()
		{
			for (int i = 0; i < this.dgvValveInfo.RowCount; i++)
			{
				this.valveSet[i].ResCoefLocal = double.Parse(this.dgvValveInfo.Rows[i].Cells[2].Value.ToString());
				this.valveSet[i].ResistanceLocal = double.Parse(this.dgvValveInfo.Rows[i].Cells[3].Value.ToString());
			}
		}

		private IList<ValveAndFitting> valveSet;
	}
}
